// pages/common/interaction-list/index.js
import Config from "../../../main.config";
import {getStorageSync} from "../../../utils/Storage";
import {
    getStudentFocusList,
    getStudentFansList,
    getPersonnelFocusList,
    getPersonnelFansList,
} from "../../../service/Collection";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        //招聘信息
        interaction: {
            loading: false,
            reset: false,
            error: false,
            total: -1,
            list: []
        },
        userTypeName: ""
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let registerId = getStorageSync(Config.storage.registerInfo + ".id");
        let search = {}, title = "互动列表";
        switch (options.name) {
            case "studentFans":
                search.objectId = registerId;
                title = "我的粉丝";
                break
            case "studentFocus":
                title = "我的关注";
                search.userId = registerId;
                break
            case "personnelFans":
                title = "我的粉丝";
                search.objectId = registerId;
                break
            case "personnelFocus":
                title = "我的关注";
                search.userId = registerId;
                break
            default:
                break
        }
        wx.setNavigationBarTitle({title});
        this.data._search = search;
        this.data._options = options;
        this.setData({
            "interaction.loading": true,
            userTypeName: options.name
        });
    },

    /*
    * 列表加载
    * */
    onLoadList: async function (e) {
        try {
            let res = null;
            let paging = e.detail.paging;
            let options = this.data._options;
            let list = this.data.interaction.list;
            let isReset = this.data.interaction.reset;
            let search = this.data._search;
            let postData = Object.assign(search, {
                page: paging.page,
                limit: paging.pageSize
            });
            switch (options.name) {
                case "studentFans":
                    res = await getStudentFansList(postData);
                    break
                case "studentFocus":
                    res = await getStudentFocusList(postData);
                    break
                case "personnelFans":
                    res = await getPersonnelFansList(postData);
                    break
                case "personnelFocus":
                    res = await getPersonnelFocusList(postData);
                    break
            }
            let newList = res.data.map(function (res) {
                let logoUrl = Config.api.downLoad + "?fileId=" + (res.staffLogo || res.studentLogo);
                return {
                    toId: res.userId || res.objectId,
                    logo: logoUrl,
                    name: res.staffName || res.studentName,
                    position: res.position || "",
                    professionalName: res.professionalName || "",
                    schoolName: res.schoolName,
                    companyName: res.companyName || "",
                    schooling: res.schooling || ""
                }
            });
            list = isReset ? newList : list.concat(newList);
            this.setData({
                "interaction.list": list,
                "interaction.total": res.count
            });
        } catch (e) {
            console.log(e)
            this.setData({
                "interaction.error": true
            })
        } finally {
            this.setData({
                "interaction.reset": false,
                "interaction.loading": false
            })
        }
    },


    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {
        this.setData({"interaction.loading": true})
    },

    /*
    * 打开详情
    * */
    bindNavigatorToDetail: function (e) {
        let options = this.data._options;
        let id = e.currentTarget.dataset.id;
        if (options.name === "personnelFans" || options.name === "personnelFocus"){return false}
        wx.navigateTo({
            url: "/packageStudent/pages/detail/index?id=" + id
        })
    }
})